package com.mxw.common.validator;

import com.mxw.common.valid.ListValue;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.lang.annotation.Annotation;
import java.util.HashSet;

public class ListValueConstraintValidator implements ConstraintValidator<ListValue, Integer> {

    private HashSet<Integer> set = new HashSet();

    //初始化方法
    public void initialize(ListValue constraintAnnotation) {
       //获取注解上的各个值
        int[] vals = constraintAnnotation.vals();
        for (int val : vals) {
            set.add(val);
        }
    }

    //判断请求的值是否包含在注解上的值
    public boolean isValid(Integer integer, ConstraintValidatorContext constraintValidatorContext) {
        return set.contains(integer);
    }
}
